package org.algorithm.List;

import java.util.function.Consumer;
import java.util.regex.Pattern;

public class SinglyLinkedList
{
    private Node head = null;

    private static class Node
    {
        int value ;
        Node next;

        public Node(int value,Node next)
        {
            this.value=value;
            this.next=next;
        }
    }

    //头插法
    public void addFirst(int value)
    {
        head=new Node(value,head);
    }

    //尾插法
    public void addLast(int value)
    {

        Node last = findLast();

        if (last==null)
        {
            addFirst(value);

            return;
        }

        last.next=new Node(value,null);
    }

    //找到链表最后一个节点
    private Node findLast()
    {

        if (head==null)
        {
            return  null;
        }

       Node p  = head;
        while (p.next!=null)
        {
            p=p.next;
        }
        return  p ;
    }

//    public void loop()
//    {
//        Node node = head;
//
//        while (node!=null)
//        {
//            System.out.println(node.value);
//            node=node.next;
//        }
//    }


        private void recursion(Node node)
        {
            if (node ==null)
            {
                return;
            }


            System.out.println("before:"+node.value);
            recursion(node.next);
            System.out.println("after:"+node.value);

        }

        public void loop2()
        {
            recursion(head);
        }

        //遍历
        public void loop(Consumer<Integer> consumer)
        {
            Node node = head;

            while (node!=null)
            {
                consumer.accept(node.value);
                node=node.next;
            }
        }

        //根据index找到对应的Node
        private Node findNode(int index)
        {
            int i =0;
            for (Node p =head; p!=null; p=p.next, i++) {
                if (i==index)
                {
                    return p;
                }
            }
            return null;
        }

        //根据index找到对应的Node的Value
        public int get(int index)
        {
            Node node = findNode(index);

            if (node==null)
            {
                throw new IllegalArgumentException();
            }else {
                return node.value;
            }
        }

        //根据index插入到对应的位置
        public void insert(int index, int value)
        {
            if (index == 0)
            {
                addFirst(value);
                return;
            }

            Node prev = findNode(index - 1);

            if (prev==null)
            {
                throw new IllegalArgumentException("index不正确");
            }else {
               prev.next=new Node(value,prev.next);
            }

        }

        //删除list的第一个节点
        public void removeFirst()
        {
            if (head==null)
            {
                throw  new IllegalArgumentException("head节点为空，无法被删除");
            }

            head=head.next;
        }

        //删除指定位置的节点
        public void remove(int index)
        {
            if (index==0)
            {
                removeFirst();
                return;
            }

            Node prev = findNode(index-1);
            if (prev==null)
            {
                throw  new IllegalArgumentException("无法被删除");
            }
            Node removed = prev.next;

            if (removed==null)
            {
                throw  new IllegalArgumentException("无法被删除");
            }

            prev.next= removed.next;

        }


}
